package telnetserver;

import java.util.ArrayList;

public class Chat {
    private Telnet telnet;
    private Object ret;
    private ArrayList<User> users;
    private String skin;
    private String[] buttons = { "SEND", "EXIT" };
    private String[] test;
    
    public Chat(Telnet t, Object r) {
        telnet = t;
        ret = r;
        users = new ArrayList<User>();
        test = new String[0];
        
        String s = "";
        for(int i = 0; i < 67; i++) {
            s += "-";
        }
        
        s = "+" + s + "+----------+";
        
        skin = t.skin(1, 1, 1, s);
        for (int i = 2; i < 22; i++) {
            skin += t.skin(1, i, "|");
            skin += t.skin(69, i, "|");
            skin += t.skin(80, i, "|");
        }
        
        skin += t.skin(70, 2, "Userlist:");
        
        skin += t.skin(3, 21, "Menu:");
        
        skin += t.skin(1, 22, s);
        skin += t.skin(1, 23, "| SEND >");
        skin += t.skin(80, 23, "|");
        
        s = "";
        for (int i = 0; i < 78; i++) {
            s += "-";
        }
        
        skin += t.skin(1, 24, "+" + s + "+");
    }
    
    public void addUser(User u) {
        u.setCanPress(true);
        u.setCanWrite(true);
        u.setMaxLength(600);
        u.setFix(10, 23, 70);
        users.add(u);
        
        String tot = "";
        for (String s : u.getChat()) {
            tot += s;
        }
        if (tot.length() == 0) {
            u.addchat("__          __  ____   _      ____   _____   _    _   ____");
            u.addchat("\\ \\        / / |  __| | |    |  __| |  _  | | \\  / | |  __|");
            u.addchat(" \\ \\  /\\  / /  | |__  | |    | |    | | | | |  \\/  | | |__");
            u.addchat("  \\ \\/  \\/ /   |  __| | |    | |    | | | | | \\  / | |  __|");
            u.addchat("   \\  /\\  /    | |__  | |__  | |__  | |_| | | |\\/| | | |__");
            u.addchat("    \\/  \\/     |____| |____| |____| |_____| |_|  |_| |____|");
            
            u.addchat("");
            u.addchat("");
        }
        
        
        u.setMsg(u.getNick());
        
        u.prepare(skin);
        
        u.setButtons(test, buttons);
        u.fixButtonsXY(70, 3, 1, 10, 21, 5);
        u.drawButtons();
        u.prepare(10, 23, "");
        u.send();
        
        
        for (User u2: users) {
            u.addButton(0, u2.getNick());
            if (u != u2) u2.addButton(0, u.getNick());
            u2.addchat(u.getNick() + " entered the chat.");
            printChat(u2);
        }
    }
    
    public void remUser(User u) {
        String s = u.getNick();
        users.remove(u);
        for (User u2 : users) {
            for (int i = 3; i < 20; i++) {
                u2.prepare(70, i, "          ");
            }
            u2.send();
            u2.remButton(0, s);
            u2.addchat(s + " left the chat.");
            printChat(u2);
        }
    }
    
    public void read(User u, String s) {
        if (u.getButton()[0] == 0 && s.trim().length() > 0) {
            users.get(u.getButton()[1]).addchat("*P* > " + u.getNick() + ": " + s);
            u.addchat("*P* < " + users.get(u.getButton()[1]).getNick() + ": " + s);
            printChat(u);
            printChat(users.get(u.getButton()[1]));
            u.prepare(10, 23, "                                                                      ");
            u.prepare(10, 23, "");
            u.send();
        } else{
            switch (u.getButton()[1]) {
                case 0:
                    if (s.trim().length() > 0) {
                        s = u.getNick() + ": " + s;
                        for (User u2 : users) {
                            String s2 = s;
                            while (s2.length() > 67) {
                                u2.addchat(s2.substring(0, 67));
                                s2 = s2.substring(67, s2.length());
                            }
                            u2.addchat(s2);
                            printChat(u2);
                        }
                        u.prepare(10, 23, "                                                                      ");
                        u.prepare(10, 23, "");
                        u.send();
                        
                    }
                    break;
                case 1:
                    remUser(u);
                    telnet.changeMode(u, ret);
                    break;
            }
        }
    }
    
    public void buttonPressed(User u, int i) {
        
    }
    
    public void printChat(User u) {
        for (int i = 2; i < 20; i++) {
            u.prepare(2, i, "                                                                   ");
        }
        
        String[] chat = u.getChat();
        
        for (int i = 2; i < chat.length; i++) {
            u.prepare(2, i, chat[i]);
        }
        
        u.prepare(10 + u.getInLength(), 23, "");
        
        u.send();
    }
}
